function [y_guess, confidence ] = blr_classify( data, w, class, displayFlag )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

pts = data(:,1:3);
id = data(:,4);
label = data(:,5);
features = data(:,6:end);

numPoints = length(label);

% Possible labels
VEG = 1004;
WIRE = 1100;
POLE = 1103;
GROUND = 1200;
FACADE = 1400;

% Decide which class we want to classify
CLASS_LABEL = class;

% Label all the training data based on the provided label. 1 for belonging
% to that class and -1 for not belonging to that class. So for example, we
% can split all the points into VEG and not VEG.
y = -1*ones(numPoints,1);
y(label==CLASS_LABEL) = 1;

% Apply this weight vector to the training set to see how we would have
% done in retrospect
y_guess = w'*features';

confidence = abs(y_guess);
confidence(confidence>1) = 1;

y_guess(y_guess>=0) = 1;
y_guess(y_guess<0) = -1;


if (displayFlag)
    % Show the BLR guess for the point's label compared to the true label
    figure
    plot(y_guess(1:numPoints),'r.');
    hold on
    plot(y(1:numPoints),'g.')
    legend('Estimated Label', 'True Label')
    title(['BLR estimated labels for CLASS:' num2str(CLASS_LABEL)]);
    
    % Threshold the estimated labels at zero and calculate percent correct
    y_guess(y_guess<0) = -1;
    y_guess(y_guess>=0) = 1;
%     total_correct = sum((y == y_guess'));
%     percent_correct = total_correct/numPoints
    
    %% Colorize and display
    colors = zeros(numPoints, 3);
    
    % Generate the labeling for the true points
    r = find(label == VEG);
    colors(r,:) = repmat([0 1 0], length(r), 1);
    
    r = find(label == WIRE);
    colors(r,:) = repmat([255 165 0]/255, length(r), 1);
    
    r = find(label == POLE);
    colors(r,:) = repmat([1 1 0], length(r), 1);
    
    r = find(label == GROUND);
    colors(r,:) = repmat([0 0 1], length(r), 1);
    
    r = find(label == FACADE);
    colors(r,:) = repmat([1 1 1], length(r), 1);
    
    % showPointCloudWithVrml('../results/true_points.wrl', [pts colors]);
    
    
    % Generate the labeling for correctly and incorrectly labeled points
    colors = ones(numPoints, 3);
    wrong_pts = find(y ~= y_guess');
    colors(wrong_pts,:) = repmat([1 0 0], length(wrong_pts), 1);
    showPointCloudWithVrml('../results/BLR.wrl', [pts colors]);
    
end

y_guess(y_guess<0) = 0;

y_guess =y_guess';
end

